<?php

vendor('url');

class MainMenuComponent extends Object
{
    protected static $breadcrumbs;
    protected static $current;
    protected static $active;
    protected static $path;
    protected $controller;

    protected static $tree;
    protected static $menu;
    protected static $indexes;

    function startup(&$controller)
    {
        $this->controller =& $controller;

        if (is_null(self::$tree)) {
            self::$tree = array(array(
                'url'           => '/',
                'controller'    => 'pages',
                'action'        => 'display',
                'pass'          => array('home'),
                'title'         => __d('menu', 'Main page', true),
                'access'        => '*',
                'items'         => array(
                array(
                    'url'           => 'logout/',
                    'controller'    => 'auth',
                    'action'        => 'logout',
                    'tags'          => 'top auth',
                    'position'      => 100,
                    'title'         => __d('auth', 'logout',true),
                    'access'        => 'register',
                ),
                array(
                    'url'           => 'login/',
                    'controller'    => 'auth',
                    'action'        => 'login',
                    'name'          => 'login',
                    'ident'         => 'login',
                    'tags'          => 'top auth',
                    'position'      => 20,
                    'title'         => __d('auth', 'enter',true),
                    'access'        => 'guest',
                ),
                array(
                    'url'           => 'clients/',
                    'controller'    => 'clients',
                    'action'        => 'list_of',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Clients', true),
                    'access'        => 'active & admin',
                    'items'         => array(
                        array(
                            'url'           => 'new',
                            'controller'    => 'clients',
                            'action'        => 'create',
                            'title'         => __d('menu', 'create', true),
                            'access'        => 'active & admin',
                        ),
                    ),
                ),
                array(
                    'url'           => 'client-admins/',
                    'controller'    => 'client_admins',
                    'action'        => 'list_of',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Client-admins', true),
                    'access'        => 'active & admin',
                    'items'         => array(
                        array(
                            'url'           => 'new',
                            'controller'    => 'client_admins',
                            'action'        => 'create',
                            'title'         => __d('menu', 'create', true),
                            'access'        => 'active & admin',
                        ),
                    ),
                ),
                array(
                    'url'           => 'billing/',
                    'controller'    => 'passes',
                    'action'        => 'list_of',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Billing', true),
                    'access'        => 'active & admin',
                    'items'         => array(
                        array(
                            'url'           => 'statistics/',
                            'controller'    => 'passes',
                            'action'        => 'statistics',
                            'tags'          => 'second_main',
                            'title'         => __d('passes', 'statistics', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'export/',
                                    'url_params'    => array('time' => strval(time())),
                                    'controller'    => 'passes',
                                    'action'        => 'statistics_export',
                                    'tags'          => 'second_main',
                                    'title'         => __d('passes', 'export_to_csv', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'export/',
                            'url_params'    => array('time' => strval(time())),
                            'controller'    => 'passes',
                            'action'        => 'list_of_export',
                            'tags'          => 'second_main',
                            'title'         => __d('passes', 'export_to_csv', true),
                            'access'        => 'active & admin',
                        ),
                    ),
                ),
                array(
                    'url'           => 'log/',
                    'controller'    => 'passes',
                    'action'        => 'list_of',
                    'tags'          => 'top_menu',
                    'title'         => __d('menu', 'view_passes', true),
                    'access'        => 'active & client_admin',
                    'items'         => array(
                        array(
                            'url'           => 'statistics/',
                            'controller'    => 'passes',
                            'action'        => 'statistics',
                            'tags'          => 'second_main',
                            'title'         => __d('passes', 'statistics', true),
                            'access'        => 'active & client_admin',
                            'items'         => array(
                                array(
                                    'url'           => 'export/',
                                    'url_params'    => array('time' => strval(time())),
                                    'controller'    => 'passes',
                                    'action'        => 'statistics_export',
                                    'tags'          => 'second_main',
                                    'title'         => __d('passes', 'export_to_csv', true),
                                    'access'        => 'active & client_admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'export/',
                            'url_params'    => array('time' => strval(time())),
                            'controller'    => 'passes',
                            'action'        => 'list_of_export',
                            'tags'          => 'second_main',
                            'title'         => __d('passes', 'export_to_csv', true),
                            'access'        => 'active & client_admin',
                        ),
                    ),
                ),
                array(
                    'url'           => 'sending-log/',
                    'controller'    => 'sendings',
                    'action'        => 'list_of',
                    'tags'          => 'top_menu',
                    'title'         => __d('menu', 'view_emails', true),
                    'access'        => 'active',
                ),
                array(
                    'url'           => 'emails-queue/',
                    'controller'    => 'emails',
                    'action'        => 'list_of',
                    'title'         => __d('menu', 'view_emails_queue', true),
                    'access'        => 'active',
                ),
                array(
                    'url'           => 'candidates/',
                    'controller'    => 'candidates',
                    'action'        => 'list_of',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Candidates for admin', true),
                    'access'        => 'active & admin',
                    'items'         => array(
                        array(
                            'url'           => 'groups/',
                            'controller'    => 'candidate_groups',
                            'action'        => 'list_of',
                            'tags'          => 'second_main',
                            'title'         => __d('menu', 'Candidate groups', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'candidate_groups',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'new',
                            'controller'    => 'candidates',
                            'action'        => 'create',
                            'title'         => __d('menu', 'create_candidate', true),
                            'access'        => 'active & admin',
                        ),
                        array(
                            'url'           => 'export',
                            'url_params'    => array('time' => strval(time())),
                            'controller'    => 'candidates',
                            'action'        => 'export',
                            'title'         => __d('menu', 'export_to_csv', true),
                            'access'        => 'active & admin',
                        ),
						array(
                            'url'           => '/reports/consolidated/',
                            'controller'    => 'reports',
                            'action'        => 'consolidate_list',
                            'tags'          => 'second_main',
                            'title'         => __d('menu', 'Consolidated reports', true),
                            'access'        => 'active & admin',
                            'items'         => array(
        						array(
                                    'url'           => 'new',
                                    'controller'    => 'reports',
                                    'action'        => 'consolidate_create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                    ),
                ),
                array(
                    'url'           => 'lists/',
                    'controller'    => 'lists',
                    'ident'         => 'lists',
                    'action'        => 'index',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Lists', true),
                    'access'        => 'active & admin',
                    'items'         => array(
                        array(
                            'url'           => '/admins/',
                            'controller'    => 'admins',
                            'action'        => 'list_of',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Administrators', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'admins',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => '/tools/',
                            'controller'    => 'tests',
                            'action'        => 'tools_and_reports_list',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Tools', true),
                            'access'        => 'active & admin',
                        ),
                        array(
                            'url'           => 'regions/',
                            'controller'    => 'regions',
                            'action'        => 'list_of',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Regions', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'regions',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'positions/',
                            'controller'    => 'positions',
                            'action'        => 'list_of',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Positions', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'positions',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'ages/',
                            'controller'    => 'ages',
                            'action'        => 'list_of',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Ages', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'ages',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'educations/',
                            'controller'    => 'educations',
                            'action'        => 'list_of',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Educations', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'educations',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'seniorities/',
                            'controller'    => 'seniorities',
                            'action'        => 'list_of',
                            'tags'          => 'compendium',
                            'title'         => __d('menu', 'Seniorities', true),
                            'access'        => 'active & admin',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'seniorities',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & admin',
                                ),
                            ),
                        ),
                    ),
                ),
                array(
                    'url'           => 'articles/',
                    'controller'    => 'articles',
                    'action'        => 'list_of',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Articles', true),
                    'access'        => 'active & admin',
                    'items'         => array(
                        array(
                            'url'           => 'new',
                            'controller'    => 'articles',
                            'action'        => 'create',
                            'title'         => __d('menu', 'create', true),
                            'access'        => 'active & admin',
                        ),
                    ),
                ),
                array(
                    'url'           => 'candidates/',
                    'controller'    => 'candidates',
                    'action'        => 'list_of',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Candidates', true),
                    'access'        => 'active & CANDIDATES',
                    'items'         => array(
                        array(
                            'url'           => 'groups/',
                            'controller'    => 'candidate_groups',
                            'action'        => 'list_of',
                            'tags'          => 'second_main',
                            'title'         => __d('menu', 'Candidate groups', true),
                            'access'        => 'active & CANDIDATES',
                            'items'         => array(
                                array(
                                    'url'           => 'new',
                                    'controller'    => 'candidate_groups',
                                    'action'        => 'create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & CANDIDATES',
                                ),
                            ),
                        ),
                        array(
                            'url'           => 'new',
                            'controller'    => 'candidates',
                            'action'        => 'create',
                            'title'         => __d('menu', 'create_candidate', true),
                            'access'        => 'active & CANDIDATES',
                        ),
                        array(
                            'url'           => 'export',
                            'url_params'    => array('time' => strval(time())),
                            'controller'    => 'candidates',
                            'action'        => 'export',
                            'title'         => __d('menu', 'export_to_csv', true),
                            'access'        => 'active & CANDIDATES',
                        ),
                    ),
                ),
                array(
                    'url'           => 'admining/',
                    'controller'    => 'candidates',
                    'action'        => 'admining',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Admining', true),
                    'access'        => 'active & CANDIDATES',
                ),
                array(
                    'url'           => 'reports/',
                    'controller'    => 'candidates',
                    'action'        => 'reports',
                    'tags'          => 'main',
                    'title'         => __d('menu', 'Reports', true),
                    'access'        => 'active & (REPORTS | EMAIL_ON_REPORT)',
                    'items' => array(
						array(
                            'url'           => 'consolidated/',
                            'controller'    => 'reports',
                            'action'        => 'consolidate_list',
                            'tags'          => 'second_main',
                            'title'         => __d('menu', 'Consolidated reports', true),
                            'access'        => 'active & (REPORTS | EMAIL_ON_REPORT)',
                            'items'         => array(
        						array(
                                    'url'           => 'new',
                                    'controller'    => 'reports',
                                    'action'        => 'consolidate_create',
                                    'title'         => __d('menu', 'create', true),
                                    'access'        => 'active & (REPORTS | EMAIL_ON_REPORT)',
                                ),
                            ),
                        ),
						array(
                            'url'           => 'consolidated/new',
                            'controller'    => 'reports',
                            'action'        => 'consolidate_create',
                            'title'         => __d('menu', 'Create consolidated report', true),
                            'access'        => 'active & (REPORTS | EMAIL_ON_REPORT)',
                        ),
                    )
                ),
                array(
                    'url'           => 'help/',
                    'controller'    => 'articles',
                    'action'        => 'index',
                    'tags'          => 'main articles help',
                    'title'         => __d('menu', 'help', true),
                    'access'        => 'client_admin',
                ),
                ),
            )
         );

        }
    }

    public function inite()
    {
        $this->_prepare();
        $this->_getCurrentItem($this->controller);
        $this->_getHightlightMenu();
    }

    public function &items()
    {
        return self::$menu;
    }

    function &getMainItems()
    {
        return self::$menu[0]->items;
    }

    function &getLeftMenu()
    {
        $current =& $this->getCurrentItem();
        if (isset($current)) {
            if ($current && in_array('main', $current->tags) || in_array('second_main', $current->tags) || in_array('top_menu', $current->tags)) {
                return $current;
            }
        }
        $tmp = null;
        return $tmp;
    }

    function &getBreadcrumbs()
    {
        return self::$breadcrumbs;
    }

    function setActive(&$item)
    {
        if (!isset(self::$active) && !isset(self::$current) && isset($item)) {
            self::$active =& $item;
        }
    }

    function &addItem(&$item, &$parent)
    {
        return $this->_prepareRecursive($item, $parent);
    }

    function &findItem($url)
    {
        if (is_null(self::$menu)) {
            $this->_prepare();
        }
        foreach (self::$indexes as &$item) {
            if (! $item->our || ! $item->base) {
                continue;
            }
            if ($item->url == $url) {
                return $item;
            }
        }
        $null = null;
        return $null;
    }

    function setCurrent(&$item)
    {
        if (!isset(self::$active) && !isset(self::$current) && isset($item)) {
            self::$current =& $item;
            self::$current->current = true;
        }
    }

    function &getCurrentItem()
    {
        return self::$current;
    }

    protected function _prepare()
    {
        if (is_null(self::$menu)) {
            for ($i = 0; $i < count(self::$tree); $i++) {
                $null = null;
                $this->_prepareRecursive(self::$tree[$i], $null);
            }
        }
    }

    protected function &_prepareRecursive(&$item, &$parent)
    {
        $null = null;
        if (! isset($item['access'])) {
            $item['access'] = 'NONE';
        }
        if (! Auth::Check($item['access'])) {
            return $null;
        }
        $tmp_item = $item;
        if (isset($tmp_item['items'])) {
            unset($tmp_item['items']);
        }
        $obj =& new MenuItem($tmp_item);

        if (is_null($parent)) {
            self::$menu[] =& $obj;
            $obj =& self::$menu[count(self::$menu) - 1];
        } else {
            $parent->items[] =& $obj;
            $obj =& $parent->items[count($parent->items) - 1];
            $obj->parent =& $parent;
            if (strpos($obj->url, '/') !== 0) {
                if (strpos($obj->url, '://') === false) {
                    $obj->url = $obj->parent->url . $obj->url;
                } else {
                    $obj->our = false;
                }
            }
        }

        if (! empty($obj->url_params)) {
            $tmp_url = new URL($obj->url);
            foreach ($obj->url_params as $pname => $pvalue) {
                $tmp_url = new URL($tmp_url->setParam($pname, $pvalue));
            }
            $obj->url_with_params = $tmp_url->url;
        } else {
            $obj->url_with_params = $obj->url;
        }

        self::$indexes[] =& $obj;
        $obj->id = count(self::$indexes);

        $tags = preg_split("/\\s+/s", $obj->tags, null, PREG_SPLIT_NO_EMPTY);
        $obj->tags = array();
        foreach ($tags as $tag) {
            $obj->tags[$tag] = true;
        }

        if (isset($item['items'])) {
            for ($i = 0; $i < count($item['items']); $i++) {
                $this->{__FUNCTION__}($item['items'][$i], $obj);
            }
        }

        return $obj;
    }

    function isCurrentMenu($menu, $params, $pass)
    {
        $result = true;
        if (isset($params['controller'])) {
            $result &= $menu->controller == $params['controller'];
        } else {
            return false;
        }
        if (isset($params['action'])) {
            $result &= $menu->action == $params['action'];
        } else {
            return false;
        }

        //$result &= ($pass === $menu->pass);
        $result &= array_intersect_assoc($menu->pass, $pass) == $menu->pass;
        $result &= array_intersect_assoc($menu->params, $params) == $menu->params;

        return $result;
    }

    function &getByTags($tags, $not_tags=array())
    {
        $result = array();
        $length = count(self::$indexes);
        for ($i = 0; $i < $length; $i++) {
            $item =& self::$indexes[$i];

            $searched = true;

            if (! empty($tags) || ! empty($not_tags)) {
                foreach ($tags as $tag) {
                    if (!isset($item->tags[$tag])) {
                        $searched = false;
                        break;
                    }
                }
                if ($searched) {
                    foreach ($not_tags as $not_tag) {
                        if (isset($item->tags[$not_tag])) {
                            $searched = false;
                            break;
                        }
                    }
                }
            } else {
                $searched = empty($item->tags);
            }

            if ($searched) {
                $result[] =& $item;
            }
        }

        return $result;
    }

    protected function _getCurrentItem(&$controller)
    {
        if (!isset(self::$current) && !isset(self::$active)) {
            foreach (self::$indexes as &$item) {
                if (! $item->our || ! $item->base) {
                    continue;
                }
                $pass = isset($controller->params['pass']) && is_array($controller->params['pass']) ? $controller->params['pass'] : array();
                if ($this->isCurrentMenu($item, $controller->params, $pass)) {
                    self::$current =& $item;
                    self::$current->current = true;
                }
            }
        }
    }

    protected function _getHightlightMenu()
    {
        if (! isset(self::$breadcrumbs)) {
            self::$breadcrumbs = array();

            if (isset(self::$current)) {
                $_tmp_item =& self::$current;
                do {
                    $_tmp_item->active = true;
                    array_unshift(self::$breadcrumbs, $_tmp_item);
                } while (! is_null($_tmp_item =& $_tmp_item->parent));
            } elseif(isset(self::$active)) {
                $_tmp_item =& self::$active;
                do {
                    $_tmp_item->active = true;
                    array_unshift(self::$breadcrumbs, $_tmp_item);
                } while (! is_null($_tmp_item =& $_tmp_item->parent));
            } else {
                self::$breadcrumbs = array(&self::$menu[0]);
            }
        }
    }

    function toCaption($str)
    {
        $result = $str;
        $tmp_result = wordwrap($str, 40, "\n", 1);
        if (($pos = strpos($tmp_result, "\n")) !== false) {
            $result = substr($tmp_result, 0, $pos) . '…';
        }
        return $result;
    }

}

class MenuItem extends Object
{
    public $id          = null;
    public $ident       = null;
    public $base        = true;
    public $url         = '';
    public $tags        = '';
    public $position    = 0;
    public $name        = '';
    public $plugin      = '';
    public $controller  = '';
    public $action      = '';
    public $params      = array();
    public $pass        = array();
    public $args        = array();
    public $title       = '';
    public $current     = false;
    public $active      = false;
    public $items       = array();
    public $parent      = null;
    public $our         = true;
    public $access      = 'NONE';
    public $data        = array();
    public $url_params  = array();
    public $url_with_params = '';

    function __construct($params)
    {
        foreach ($params as $name => $value) {
            if (property_exists($this, $name)) {
            	$this->$name = $value;
            } elseif ($name == 'callback') {
        		if(is_callable($value)) {
            		$this->title = call_user_func($value);
        		}
            }

        }
    }
}

?>